<?php
namespace app\shops\controller;

use think\Db;
use think\Model;

/**
 * 班级管理
 */
class Classes extends Base
{
    /**
     * @throws
     * 班级列表
     * @return [type] [description]
     */
    public function index()
    {
        // 获取配置信息
        $conf = config();
        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];


        // 获取前台传过来的门店查询条件
        $store_id = input('get.keywords1') ? : "";
        $this->assign('keywords1',$store_id);
        $pageParam['query']['store_id'] = $store_id;

        // 列表显示所有班级
        $list = Db("classes")->alias('c')->join('store sr', 'c.store_id=sr.store_id')
            ->field('c.id as class_id, c.class_name, c.status, sr.store_id, sr.store_name')
            ->where('c.shop_id',$this->shop_id)
            ->where('sr.store_id','like',"%{$store_id}%")
            ->order("c.id desc")->paginate($pageSize);

        $store_list = Db('store')
            ->where('shop_id',$this->shop_id)
            ->field('store_id, store_name')->select();

        $this->assign([
            'total'       => $list->total(),
            'page'        => $list->render(),
            'list'        => $list,
            'store_list'  =>$store_list
        ]);

        return $this->fetch();
    }

    /**
     * 添加班级
     */
    public function add()
    {

        if(request()->isPost()){

            $data['store_id']       = input('store_id');
            $data['class_name']     = input('class_name');
            $data['shop_id']        = $this->shop_id;

            $data['class_name'] = str_replace("，",",",$data['class_name']);
            $data['class_name'] = explode(',',$data['class_name']);
            foreach ($data['class_name'] as $k=>$v){
                if ($v!=null){
                    $insert_data[] = [
                        'class_name'=>$v,
                        'store_id'=>$data['store_id'],
                        'shop_id'=>$data['shop_id']
                    ];
                }

            }


            $re = Db("classes")->insertAll($insert_data);

            if($re){
                $this->success("添加成功");
            }else{
                $this->error("网络错误");
            }

        }
        // 获取门店列表
        $store_list = Db('store')->where('shop_id',$this->shop_id)->field('store_id,store_name')->select();

        $this->assign('store_list', $store_list);

        return $this->fetch();
    }

    /**
     * @throws
     * 编辑班级
     */
    public function edit()
    {

        $class_id = input('class_id');

        // 更新数据
        if(request()->isPost()){

            input('class_name')     == null || $data['class_name']   = input('class_name');
            input('id')             == null || $data['id']     = input('class_id');


            $re = Db("classes")->where('id',input('class_id'))->update($data);
            if($re===false){
                $this->error("编辑失败");
            }
            $this->success("编辑成功");

        }

        // 班级详情
        $class_info = Db("classes")->alias('c')->join('store s','c.store_id=s.store_id')
            ->where('c.id',$class_id)->field('c.class_name, c.store_id, s.store_name')->find();

        $this->assign([
            'class_info' => $class_info,
            'class_id'   => $class_id
        ]);


        return $this->fetch();
    }

    /**
     *班级的状态（开启/关闭）
     */
    public function is_use(){

        if (request()->isAjax()){
            // 获取参数
            $class_id    = input('class_id');
            $status      = input('status');

            // 更新班级状态
            $res = Db('classes')->where('id', $class_id)->update(['status' => $status]);
            // 返回结果
            if($res){
                $this->success("成功");
            }else{
                $this->error("网络异常");
            }
        }

    }


}
